package com.lw.json;

import com.alibaba.fastjson.JSON;
import com.lw.json.pojo.Student;
import com.lw.json.service.JsonGrammar;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * @Description:
 * @ClassName: FastJsonTests
 * @Author: lanwei
 * @Date: 2022/12/13 16:26
 */
@SpringBootTest
@RunWith(SpringJUnit4ClassRunner.class)
public class FastJsonTests {

    @Autowired
    JsonGrammar jsonGrammar;

    @Test
    public void javaBeanToJson(){

        Student student = new Student(100,"jay",36,1,"中国台湾");
        //将java对象转化为json串
        String string = JSON.toJSONString(student);
        System.out.println(string);
    }

    //将数组转化为json串
    @Test
    public void arrayToJson(){
        Student student1 = new Student(100,"jay1",36,1,"中国台湾");
        Student student2 = new Student(101,"jay2",37,1,"中国台湾");
        Student student3 = new Student(102,"jay3",38,1,"中国台湾");
        Student student4 = new Student(103,"jay4",39,1,"中国台湾");

        Student[] students = {student1,student2,student3,student4};
        String jsonStr = JSON.toJSONString(students, true);

        System.out.println(jsonStr);

    }

    //将list集合转化为json
    @Test
    public void listToJson(){
        Student student1 = new Student(100,"jay1",36,1,"中国台湾");
        Student student2 = new Student(101,"jay2",37,1,"中国台湾");
        Student student3 = new Student(102,"jay3",38,1,"中国台湾");
        Student student4 = new Student(103,"jay4",39,1,"中国台湾");

        List<Student> studentList = new ArrayList<Student>();
        //使用Collections将数据一次放进list中
        Collections.addAll(studentList,student1,student2,student3,student4);
        String jsonStr = JSON.toJSONString(studentList);

        System.out.println(jsonStr);

    }


    /**
     * 工作中json反序列化遇到频率较高，所以作为重点
     *
     * 1.JSON.parseObject();
     *      注意反序列化为java对象是，必须要有无参构造方法
     * 2.JSON.paresArray();
     */
    @Test
    public void jsonToJavaBean(){
        String jsonStr = "{\"id\":100,\"username\":\"jay1\",\"age\":36,\"sex\":1}";
        Student student = JSON.parseObject(jsonStr, Student.class);
        System.out.println(student);
    }

    @Test
    public void jsonToJavaList(){
        String jsonStr = "[{\"id\":100,\"username\":\"jay1\",\"age\":36,\"sex\":1},{\"id\":101,\"username\":\"jay2\",\"age\":37,\"sex\":1}," +
                "{\"id\":102,\"username\":\"jay3\",\"age\":38,\"sex\":1},{\"id\":103,\"username\":\"jay4\",\"age\":39,\"sex\":1}]";
        List<Student> studentList = JSON.parseArray(jsonStr, Student.class);
        System.out.println(studentList);
    }

    /**
     * idea中有一款插件可以根据json串生成对应的实体 ==> Json2Pojo
     * 使用时需要引入gson依赖
     *
     *         <dependency>
     *             <groupId>com.google.code.gson</groupId>
     *             <artifactId>gson</artifactId>
     *             <version>2.8.8</version>
     *         </dependency>
     * 在要生成实体的包上右击 new ==> Generate POJOs from JSON
     * 即可完成实体类生成！
     */

}
